<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>Document</title>
    <link rel="stylesheet" href="../../css/api.css">
    <link rel="stylesheet" href="../../css/news.css">
    <link rel="stylesheet" href="../../css/lianmai.css">
</head>

<body>
    <div class="bg" id="view" v-cloak>
        <div class="user-wrap">
            <div class="tx img-h">
                <!-- <img src="../../image/test/01.jpg" alt=""> -->
                <img :src="returnImg(ffInfo.head_portrait)" onerror="this.src='../../image/touxiang.png'" alt="" />
            </div>
            <p class="name">{{ffInfo.name}}</p>
            <p v-if="type == -1">正在等待对方接受邀请</p>
            <p v-if="type == 0">邀请您连麦</p>
            <p v-if="type == 1">正在通话中</p>
            <p class="time" v-if="type == 1">{{time}}</p>
        </div>
        <footer id="fx-footer" :class="{'flex-bt':type==0, 'flex-c':type!=0}">
            <div class="btn-wrap" onclick="refuse()">
                <i class="cancel-icon"></i>
                <button class="cancel-btn btn" v-if="type == -1">取消</button>
                <button class="cancel-btn btn" v-if="type == 0">拒绝</button>
                <button class="cancel-btn btn" v-if="type == 1">挂断</button>
            </div>
            <div class="btn-wrap" v-if="type == 0" onclick="call()">
                <i class="sub-icon"></i>
                <button class="sub-btn btn">接听</button>
            </div>
        </footer>
    </div>
</body>
<script src="../../script/api.js"></script>
<script src="../../script/jquery.js"></script>
<script src="../../script/ffkj.js"></script>
<script src="../../script/vue.js"></script>
<script src="../../script/agora.js"></script>
<script src="../../script/audio.js"></script>
<script src="../../script/chat.js"></script>
<script>
    var myuserid = $api.getStorage('userid');
    var touserid;
    var view = new Vue({
        el: '#view',
        data: {
            type: 0, // 连接情况 -1发起 0 未连接 1已连接
            status: 0, // 后端是否有推送状态过来
            imgurl: imgurl,
            ffList: [],
            ffInfo: {},
            time: '',
            types: 1, // 1语音|2视频
            myuserid: 0,
            touserid: 0,
            order: '',
            intimate: 0, // 是否需要限制亲密度 1需要 0不需要

        },
        methods: {
            _url(param, url) {
                _url(param, url);
            },
            // 返回图片路径
            returnImg(url) {
                return returnImg(url);
            },
        }
    })
    var artc;
    var audio;
    apiready = function() {
        if (!$api.getStorage('lianmai')) {
            _alert('缺少语音通话配置，无法进行语音通话', function() {
                api.closeWin();
            });
            return;
        }
        _listener('viewappear', function() {
            if ($api.getStorage('callStatus') == 0 && $api.getStorage('requestStatus') == 0) {
                api.closeWin();
            }
        })
        // 禁止熄屏
        api.setKeepScreenOn({
            keepOn: true
        });
        _listener('keyback', function() {
            refuse()
        })
        view.intimate = api.pageParam['intimate'] || 0;
        view.myuserid = $api.getStorage('userid');
        view.touserid = api.pageParam['id'];
        view.type = api.pageParam['type'];
        if (api.pageParam['orderid']) {
            // 发起方推过来的订单id
            view.order = api.pageParam['orderid'];
        }

        artc = new ARtc();
        audio = new Audio();
        audio.play({
            url: 'widget://res/tip.mp3',
            loop: 1,
        })
        audio.call()

        // 获取对方资料
        _getUser(function(ret) {
            view.ffInfo = ret.result;
        }, view.touserid, 1)

        $api.setStorage('callStatus', 1); // 正在通话中
        if (view.type == -1) {
            createOrder(function() {
                // 发起 要求后端推送B方
                sendB();
            })
        }
        // 监听 后端推送过来的 1接听/0挂断 状态
        _listener('returnRequest', function(ret) {
            console.log(JSON.stringify(ret))
            if (audio) {
                audio.stop();
            }
            var data = ret.value;
            if (data.types == view.types) {
                if (data.uid == view.touserid) { // 如果状态是当前接听用户返回的
                    if (data.status == 0) {
                        _leave()
                        _msg('对方已挂断');
                        timerWin();
                    } else if (data.status == 1) {
                        view.type = 1;
                        // 通话连接成功 开始扣费 接下来的一分钟
                        payForChat();
                        showCallTime();
                    } else if (data.status == 2) {
                        _leave()
                        _alert('对方余额不足，无法继续支持本次通话', function() {
                            _closeChat()
                        })
                    } else if (data.status == 3) {
                        _leave();
                        _alert('对方忙线中，请稍后再拨', function() {
                            _closeChat()
                        })
                    } else if (data.status == 4) {
                        _leave();
                        _msg('对方已挂断');
                        _closeChat(1)
                    }
                    view.status = 1;
                }
            }
        })
        _listener('logout', function(ret) {
            refuse(0, ret.value.uid, 1)
        })
    }

    // 加入
    function _join(uid) {
        artc.join('channel' + uid, view.myuserid);
    }
    // 离开
    function _leave() {
        artc.leave();
    }

    function stopAudio() {
        if (audio) {
            audio.stop();
        }
    }
</script>

</html>